/**
 * 事项管理 - 已录入事项
 * 版本号： v1.0.0
 */
$(function () {


    $("#myInput_List").datagrid({
        title: "已录入采购事项",
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        pagination: true,
        striped:true,
        pageSize:10,
        pageList:[10,30,50,100],
        url: CONTEXT_PATH + '/purchase/myInputList.do',
        idField: 'purId',
        columns: [
            [
                {field: 'prName', title: '项目名称', width: 160, fixed: true},
                {field: 'coName', title: '合同名称', width: 160, fixed: true},
                {field: 'coNumber', title: '合同编号', width: 140, fixed: true},
                {field: 'purCode', title: '采购单号', width: 180, fixed: true},
                {field: 'purType', title: '请购类型', width: 70, fixed: true, align:'center',
                    formatter: function (val) {
                        if (val == 1) return "<span style='color: green; font-weight: bold'>合同内</span>";
                        if (val == 2) return "<span style='color: red; font-weight: bold'>合同外</span>";
                }},
                /*{field: 'createPerson', title: '请购人', width: 90, fixed: true, align:'center'},*/
                {field: 'purPerson', title: '请购人', width: 90, fixed: true, align:'center'},
                {field: 'checkState', title: '请购状态', width: 70, fixed: true, align:'center',
                    formatter: function (val) {
                        if (val == "0") return "<span style='font-weight: bold; color: black'>未审核</span>";
                }},
                {field: 'purDate', title: '申请时间', width: 135}
            ]
        ],
        onLoadSuccess:function(data){
            if(data.rows.length > 0){
                $("#myInput_List").datagrid("selectRow", 0);
            }
        }
    });



    //定义可操作按钮
    $("#myInput_listMenu").dialog({
        title: '',
        width: 'auto',
        height: 35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });

});


/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
var addId = "";
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for (var j = 0; j < btnOptions.length; j++) {
        if (false != btnOptions[j].value) {
            falg = true;
            break;
        }
    }
    if (!falg) {
        //不存在按钮
        toolbar.push({});
    } else {
        //存在按钮
        for (var i = 0; i < btnOptions.length; i++) {
            if (false != btnOptions[i].value) {
                //添加按钮
                if ("addBtn" == btnOptions[i].key) {
                    addId = btnOptions[i].value.meId;
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            myInput_add();
                        }
                    });
                }
                //删除按钮
                if ("delBtn" == btnOptions[i].key) {
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            myInput_del();
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}


/**
 * 申请请购
 */
var addFlag = "true";
function myInput_add() {
    var row = $("#myInput_List").datagrid("getSelected");
    if (null == row) {
        $.messager.show({title: '操作提示', msg: "请选择删除采购申请！", timeout: 3000, showType: "slide"});
        return;
    };
    $("#"+addId).linkbutton('disable');
    if(addFlag=="true"){
        $("#"+addId).linkbutton('disable');
        $.ajax({
            url: CONTEXT_PATH + "/purchase/startProcess.do?purId=" + row.purId,
            type: "POST",
            success: function (data) {
                messager(data);
                $("#myInput_List").datagrid("reload", {});
                //成功后恢复请购按钮可使用
                addFlag = "false";
                $("#"+addId).linkbutton('enable');
            }
        })
    }else{
        $.messager.show({title: '操作提示', msg: "请购添加未完成，请等待！", timeout: 3000, showType: "slide"});
    }

}


/**
 * 删除
 */
function myInput_del() {

    /**

    var row = $("#myInput_List").datagrid("getSelected");
    if (null == row) {
        $.messager.show({title: '操作提示', msg: "请选择删除采购申请!", timeout: 3000, showType: "slide"});
        return;
    };
    $.messager.confirm('删除提示', '删除【 ' + row.purCode + ' 】请购单吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/business/del.do?purId=" + row.purId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#myInput_List").datagrid("reload", {});
                }
            })
        }
    });

     **/
}
